// pages/tows/tows.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //注册弹窗
        hiddenmodalput: true,
        //登录弹窗
        hiddenmodalputs: true,
        //注册按钮
        hiddenmodelbutton: false,
        //登录按钮
        hiddenmodelbuttons: false,
        //将文本隐藏
        none: "none",
        //粉丝文本
        hiddenmust: true,
        //注册提交定义变量来存储提交时获取的input的数据
        userdata: {
            username: '',
            email: '',
            password: ''
        },
        //登录提交定义储存变量
        tododata: {
            email: '',
            password: ''
        },
        //登录成功显示邮箱的储存
        emails: '',
        //显示粉丝的数量
        userfont:'',
        //显示关注的数量
        usersize: ''
    },

    //点击注册按钮弹窗框
    modalinput: function () {
        this.setData({
            hiddenmodalput: !this.data.hiddenmodalput
        })
    },
    //点击登录按钮弹出框
    modelput: function () {
        this.setData({
            hiddenmodalputs: !this.data.hiddenmodalputs
        })
    },
    //点击x关闭注册和登录弹窗
    outinput: function () {
        this.setData({
            hiddenmodalput: true,
            hiddenmodalputs: true
        })
    },
    //点击提交关闭注册和登录弹窗
    adder: function () {
        this.setData({
            hiddenmodalput: true,
            hiddenmodalputs: true,
        })
    },

   //点击关注跳转到关注页面
    guanzhu:function () {
        wx.navigateTo({
            url: '/pages/guanzhu/guanzhu',
        });
    },
    
    //点击粉丝跳转到粉丝列表
    famliy: function () {
        wx.navigateTo({
            url: '/pages/famliy/famliy',
        });
    },

    //注册
    //提交数据时自动获取输入框的值
    loginform: function (data) {
        // 给上面变量赋值
        this.data.userdata = data.detail.value,
            console.log(data.detail.value);
        //发起请求，进行传值
        wx.request({
            url: 'http://127.0.0.1:7001/api/v1/user',
            data: this.data.userdata,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            //请求成功
            success: (result) => {
                console.log(result);
                //判断状态是否正确，提示用户
                if (result.statusCode === 200) {
                    wx.showModal({
                        title: '注册成功了，宝贝'
                    })
                } else {
                    wx.showModal({
                        title: result.data.error,
                    })
                }
            }
        });
    },

    // 登录
    //点击登录按钮按
    logoform: function (env) {
        //点击登录按钮获取到用户数据
        this.data.tododata = env.detail.value,
            console.log(env.detail.value);
        //发起请求
        wx.request({
            url: 'http://127.0.0.1:7001/api/v1/user/login',
            data: this.data.tododata,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: (result) => {
                console.log(result);
                //请求成功进行判断，设置弹窗提醒用户
                if (result.statusCode == 200) {
                    wx.showModal({
                        title: '登陆成功'
                    })
                    wx.showModal({
                        title: '一定要刷新哦'
                    })
                    //请求成功把后端数据以及token保存到本地
                    wx.setStorageSync('key', result.data)
                    //点击登录按钮之后把注册和登录按钮隐藏显示邮箱和粉丝文本
                    this.setData({
                        hiddenmodelbutton: !this.data.hiddenmodelbutton,
                        hiddenmodelbuttons: !this.data.hiddenmodelbuttons,
                        hiddenmust: !this.data.hiddenmust
                    })
                    //判断是否显示如果没显示就显示邮箱,将邮箱的值
                    if (this.data.none == "none") {
                        this.setData({
                            none: "block",
                            emails: result.data.email
                        })
                    }
                } else {
                    wx.showModal({
                        title: result.data.error
                    })
                }
            }
        });
    },

    // 退出登录,
    // 清除本地存储，让按钮显示，文本、粉丝隐藏
    outlogin: function () {
        wx.clearStorageSync();
        this.setData({
            hiddenmodelbutton: !this.data.hiddenmodelbutton,
            hiddenmodelbuttons: !this.data.hiddenmodelbuttons,
            hiddenmust: !this.data.hiddenmust
        })
        if (this.data.none == "block") {
            this.setData({
                none: "none"
            })
        }
    },

    loveYou:function(){
        wx.navigateTo({
            url: '/pages/love/love',
        });
    },

    //点击个人中心查看跳转页面
    look: function () {
        //判断有没有token，如果有就弹窗框执行,就说明登录了
        var item = wx.getStorageSync('key')
        if (!item) {
            wx.showModal({
                title: '登录已失效，请重新登录',
            });
        } else {
            //跳转页面
            wx.navigateTo({
                url: '/pages/twolist/twolist'
            });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    //页面加载判断本地存储，判断按钮和文本的显示和隐藏
    onLoad: function (options) {
        var that = this
        var value = wx.getStorageSync('key')
        console.log(value);
        //如果没有存储按钮就显示，
        if (!value) {
            this.data.hiddenmodelbutton = false;
            this.data.hiddenmodelbuttons = false;
        } else {
            that.setData({
                hiddenmodelbutton: !this.data.hiddenmodelbutton,
                hiddenmodelbuttons: !this.data.hiddenmodelbuttons,
                hiddenmust: !this.data.hiddenmust
            })
            if (that.data.none == "none") {
                that.setData({
                    none: "block",
                    emails: value.username
                })
            }
        }
        //请求粉丝数量
        wx.request({
            url: 'http://127.0.0.1:7001/api/v1/subscribelist',
            data: {},
            header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': value.token, },
            method: 'GET',
            success: (result) => {
                console.log(result);
                that.setData({
                    userfont: result.data.list.length
                })
                console.log(this.data.userfont);
            },
        });
        //请求关注数量
        wx.request({
            url: 'http://127.0.0.1:7001/api/v1/channellist',
            data: {},
            header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': value.token, },
            method: 'GET',
            success: (result) => {
              console.log(result);
              that.setData({
                usersize:result.data.list.length
              })
              console.log(this.data.usersize);
            },
          });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})